package com.example.jixinlai.fragmenttest_203jxl.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jixinlai.fragmenttest_203jxl.R;
import com.example.jixinlai.fragmenttest_203jxl.adapter.ArticleAdapter;
import com.example.jixinlai.fragmenttest_203jxl.adapter.CaseAdapter;
import com.example.jixinlai.fragmenttest_203jxl.bean.ArticleBean;
import com.example.jixinlai.fragmenttest_203jxl.bean.CaseBean;
import com.example.jixinlai.fragmenttest_203jxl.iface.ArticleListener;
import com.example.jixinlai.fragmenttest_203jxl.iface.CaseListener;
import com.example.jixinlai.fragmenttest_203jxl.model.ArticleModel;
import com.example.jixinlai.fragmenttest_203jxl.model.CaseModel;

import java.util.List;

/**
 * Created by Neusoft on 2017/4/9.
 */

public class Fragment2 extends Fragment {
    private  TextView tvinfo;
    private View view=null;
    private Context context;

    private RecyclerView recyclerView;//RecyclerView控件
    private RecyclerView.LayoutManager layoutManager;//RecyclerView布局管理
    private CaseAdapter myAdapter;//RecyclerView的适配器
    private List<CaseBean> list = null;//保存数据
    CaseListener caseListener = new CaseListener() {
        @Override
        public void onResponse(List<CaseBean> beanList) {
            list = beanList;
            myAdapter.setList(list);
            myAdapter.notifyDataSetChanged();
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getActivity(), "失败:" + msg, Toast.LENGTH_SHORT).show();
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context=getActivity();
       //动态加载Fragment1的布局文件
        view=inflater.inflate(R.layout.fragment2,container,false);
        //获取布局上的控件，注意通过view获取
        tvinfo=(TextView)view.findViewById(R.id.textView8);
        tvinfo.setText("朋友");
        Toast.makeText(context, "加载Fragment朋友", Toast.LENGTH_SHORT).show();
        //注意Toast第一个参数上下文
        initRecyclerView();
        CaseModel model = new CaseModel();
        model.getResultList("case", 1, "rv1l2tidbomann2qn86nln6ur2", caseListener);
        //返回动态生成的view
        return view;
    }
    private void initRecyclerView() {
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        layoutManager = new LinearLayoutManager(getActivity());//创建线性布局
        recyclerView.setLayoutManager(layoutManager);//设置布局管理器
        recyclerView.setHasFixedSize(true);//固定每个item高度，可以提高性能
        //list=getData();//获取数据
        myAdapter = new CaseAdapter(getActivity());
        myAdapter.setList(list);//传进数据
        recyclerView.setAdapter(myAdapter);//绑定Recyclerview和Adapter

    }
}

